package com.cj.controller;
import com.cj.po.Dormitory;
import com.cj.po.Dormrepair;
import com.cj.po.PageInfo;
import com.cj.service.DormrepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Controller
    public class DormrepairController {
        // 依赖注入
        @Autowired
        private DormrepairService dormrepairService;
        /**
         * 分页查询
         * pageIndex 当前页码
         * pageSize  显示条数
         */
        @RequestMapping(value = "/findDormRepair")
        public String findDormRepair(Integer r_id, Integer d_id, String d_dormbuilding, String r_name, String reason,
                                     Date create_time, Date update_time,
                                     Integer pageIndex, Integer pageSize, Model model) {

            PageInfo<Dormrepair> pi = dormrepairService.findPageInfo(r_id,d_id,d_dormbuilding,r_name,reason,create_time,update_time,pageIndex,pageSize);
            model.addAttribute("di",pi);
            model.addAttribute("r_name",r_name);
            return "dormrepair_list";
        }
        /**
         * 导出Excel
         */
        @RequestMapping(value = "/exportdormrepairlist", method = RequestMethod.POST)
        @ResponseBody
        public List<Dormrepair> exportStudent(){
            List<Dormrepair> DormrepairList = dormrepairService.getAll();
            return DormrepairList;
        }

        /**
         * 删除学生信息
         */
        @RequestMapping( "/deleteDormRepair")
        @ResponseBody
        public String deleteDormRepair(Integer r_id) {
            int s = dormrepairService.deleteDormRepair(r_id);
            return "dormrepair_list";
        }
        /**
         * 添加学生信息
         */

        @RequestMapping(value = "/addDormRepair" ,method = RequestMethod.POST)
        @ResponseBody
        public String addDormRepair(@RequestBody Dormrepair dormrepair) {
            int s = dormrepairService.addDormRepair(dormrepair);
            return "dormrepair_list";
        }

    /**
     * 修改宿舍信息
     */
    @RequestMapping(value = "/updateDormRepair", method = RequestMethod.POST)
    public String updateDormRepair(Dormrepair dormrepair) { // 修改为 DClass
        int result = dormrepairService.updateDormRepair(dormrepair); // 更新班级信息
        return "redirect:/findDormRepair"; // 跳转到班级列表
    }

    /**
     * 根据宿舍ID查找宿舍信息
     */
    @RequestMapping("/findDormRepairById")
//    @ResponseBody
    public String findDormRepairById(Integer r_id, HttpSession session) { // 修改为 c_id
        Dormrepair dormrepair = dormrepairService.findDormRepairById(r_id); // 查找班级信息
        session.setAttribute("d", dormrepair); // 将班级信息存入会话
        return "dormrepair_edit"; // 返回班级编辑页面
    }
}
